package com.hyh.controller;

import com.hyh.common.domain.PageDTO;
import com.hyh.common.domain.PageQuery;
import com.hyh.common.domain.R;
import com.hyh.domain.dto.ItemAddDTO;
import com.hyh.domain.dto.ItemDTO;
import com.hyh.domain.po.Item;
import com.hyh.domain.query.ItemPageQuery;
import com.hyh.service.IItemService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequiredArgsConstructor
@Api(tags = "商品接口")
@RequestMapping("/items")
public class ItemController {

    private final IItemService itemService;

    @ApiOperation("查询所有商品")
    @GetMapping("/list")
    public R<List<Item>> list(){
        return R.ok(itemService.lambdaQuery().list());
    }

    @ApiOperation("添加商品")
    @PostMapping("/add")
    public R add(@RequestBody @Validated ItemAddDTO itemAddDTO){
        itemService.add(itemAddDTO);
        return R.ok();
    }

    @ApiOperation("分页查询商品")
    @GetMapping("/page")
    public R<PageDTO<ItemDTO>> queryItemByPage(ItemPageQuery itemPageQuery){
        PageDTO<ItemDTO> page = itemService.search(itemPageQuery);
        return R.ok(page);
    }

    @ApiOperation("更新商品")
    @PutMapping("/update")
    public R update(@RequestBody @Validated ItemDTO itemDTO){
        itemService.updateItem(itemDTO);
        return R.ok();
    }

    @ApiOperation("商品删除")
    @DeleteMapping("/delete/{id}")
    public R delete(@PathVariable Long id){
        itemService.removeById(id);
        return R.ok();
    }

}
